package com.caldremch.d2demo;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import javax.inject.Inject;

import dagger.Component;
import dagger.Module;
import dagger.Provides;

public class MVPActivity extends AppCompatActivity implements MainContract.View{

    @Inject
    MainPresener mainPresener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp);

        DaggerMainComponent.builder().mainModule(new MainModule(this))
                .build()
                .inject(this);

        findViewById(R.id.btn_ck).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainPresener.getData();
            }
        });



    }

    @Override
    public void updateUi() {
        Toast.makeText(this,"调用了 updateUI", Toast.LENGTH_SHORT).show();
    }
}

class MainPresener {

    private MainContract.View mView;

    @Inject
    MainPresener(MainContract.View view) {
        this.mView = view;
    }

    public void getData(){
        mView.updateUi();
    }

}


@Module
class MainModule{

    private final MainContract.View mView;

    public MainModule(MainContract.View view) {
        mView = view;
    }

    @Provides
    MainContract.View provideMainView(){
        return mView;
    }
}

@Component(modules = MainModule.class)
interface MainComponent{
    void inject(Activity activity);
}

class MainContract {

    interface View {
        void updateUi();
    }

}